import vtkXMLImageDataReader from '@kitware/vtk.js/IO/XML/XMLImageDataReader';
import vtkOBJReader from '@kitware/vtk.js/IO/Misc/OBJReader';
import vtkPDBReader from '@kitware/vtk.js/IO/Misc/PDBReader';
import vtkPLYReader from '@kitware/vtk.js/IO/Geometry/PLYReader';
import vtkJSONReader from '@kitware/vtk.js/IO/Misc/JSONReader';
import vtkXMLPolyDataReader from "@kitware/vtk.js/IO/XML/XMLPolyDataReader";
import vtkSTLReader from '@kitware/vtk.js/IO/Geometry/STLReader';
import vtkSkyboxReader from '@kitware/vtk.js/IO/Misc/SkyboxReader';
 
import extensionToImageIO from 'itk/extensionToImageIO';
import readImageArrayBuffer from 'itk/readImageArrayBuffer';
import readImageFile from 'itk/readImageFile';
import vtkITKImageReader from '@kitware/vtk.js/IO/Misc/ITKImageReader';
vtkITKImageReader.setReadImageArrayBufferFromITK(readImageArrayBuffer);
vtkITKImageReader.setReadImageArrayBufferFromITK(readImageFile);
 
import vtkITKPolyDataReader from '@kitware/vtk.js/IO/Misc/ITKPolyDataReader';
import readPolyDataArrayBuffer from 'itk/readPolyDataArrayBuffer';
import extensionToPolyDataIO from 'itk/extensionToPolyDataIO';
vtkITKPolyDataReader.setReadPolyDataArrayBufferFromITK(readPolyDataArrayBuffer)

const vtkReaderList = [
  // 因为vtk不能直接读取vtk格式,得借助itk进行转换
  {
    extension: 'vtp',
    name: 'Polydata Reader',
    vtkReader: vtkXMLPolyDataReader,
    binary: true,
  },
  {
    extension: 'vti',
    name: 'ImageData Reader',
    vtkReader: vtkXMLImageDataReader,
    binary: true,
  },
  {
    extension: 'stl',
    name: 'STL Binary Reader',
    vtkReader: vtkSTLReader,
    binary: true,
  },
  {
    extension: 'obj',
    name: 'OBJ Reader',
    vtkReader: vtkOBJReader,
    binary: false,
  },
  {
    extension: 'pdb',
    name: 'PDB Reader',
    vtkReader: vtkPDBReader,
    binary: false,
    sourceType: 'vtkMolecule',
  },
  {
    extension: 'glyph',
    name: 'Glyph Data Reader',
    vtkReader: vtkJSONReader,
    binary: false,
    sourceType: 'Glyph',
  },
  {
    extension: 'skybox',
    name: 'Skybox Data Reader',
    vtkReader: vtkSkyboxReader,
    binary: true,
    sourceType: 'Skybox',
  },
  {
    extension: 'json',
    name: 'JSON Reader',
    vtkReader: vtkJSONReader,
    binary: false,
  },
  {
    extension: 'ply',
    name: 'PLY Reader',
    vtkReader: vtkPLYReader,
    binary: true,
  }
]
const imageExtensions = Array.from(
  new Set(
      Array.from(extensionToImageIO.keys()).map((ext) => ext.toLowerCase())
  )
)

const polyDataExtensions = Array.from(
  new Set(
      Array.from(extensionToPolyDataIO.keys()).map((ext) => ext.toLowerCase())
  )
)
 
const extensions = imageExtensions.concat(polyDataExtensions);

imageExtensions.filter((e) => e !== 'dcm').forEach((extension) => vtkReaderList.push({
  extension,
  name: `${extension.toUpperCase()} Reader`,
  vtkReader: vtkITKImageReader,
  binary: true,
  fileNameMethod: 'setFileName'
}))

extensions.forEach((extension) => vtkReaderList.push({
  extension,
  name: `${extension.toUpperCase()} Reader`,
  vtkReader: vtkITKPolyDataReader,
  binary: true,
  fileNameMethod: 'setFileName'
}))

export const getVtkReaderOrKeyValue = () => {
  const obj = {}
  vtkReaderList.forEach(({ extension, name, vtkReader, sourceType, binary, fileNameMethod }) => {
    obj[extension] = {
      name,
      vtkReader,
      readMethod: binary ? 'readAsArrayBuffer' : 'readAsText',
      parseMethod: binary ? 'parseAsArrayBuffer' : 'parseAsText',
      sourceType,
      fileNameMethod,
    }
  })
  return obj
}